<?php

class Damages_Methods_Request_SetCarDamageStatus extends Damages_Methods_Request {

  /**
   *
   * @var int
   */
  public $id;

  /**
   *
   * @var int
   */
  public $statusId;

  public function __construct($params) {
    parent::__construct($params);
    $this->id = (int) $params->id;
    $this->statusId = (int) $params->statusId;
  }

  public function process() {
    $data = $this->getDbObject();
    if (!$data)
      throw new Damages_Exception_App(Damages_Codes::DB_OBJECT_NOT_FOUND);
    $initialStatusCode = $data->getDictStatuses()->getCode();
    $newStatusCode = DamagesDictStatusesQuery::create()->findOneById($this->statusId)->getCode();
    if ($initialStatusCode == DamagesDictStatuses::CODE_WAITING_FOR_PERPETRATOR_ACCEPT && $newStatusCode == DamagesDictStatuses::CODE_WAITING_FOR_INVOICE) {
      $this->sendDealerNotificationEmail($data);
    }
    $data->setStatusId($this->statusId);
    $data->save();
    $response = new Damages_Methods_Response(true);
    return $response;
  }

  /**
   * 
   * @return DamagesCarDamages
   */
  private function getDbObject() {
    $data = DamagesCarDamagesQuery::create()->findOneById($this->id);
    return $data;
  }

  private function sendDealerNotificationEmail(DamagesCarDamages $carDamage) {
    if (!$carDamage->getCarData()->getDealersRelatedByDealerId())
      return false;
    if ($carDamage->getCarData()->getDealersRelatedByDealerId()->getUserDealerss()->isEmpty())
      return false;
    $estimatedBudget = DamagesEstimatedBudgetsQuery::create()
            ->filterByCarDamageId($this->id)
            ->filterByIsDefault(1)
            ->findOne();
    if (!$estimatedBudget)
      throw new Damages_Exception_App(Damages_Codes::SEND_EMAIL_NO_DEFAULT_EST_BUDGET);
    $viewDir = Zend_Controller_Front::getInstance()->getModuleDirectory() . '/views/scripts/partials/';
    $view = new Zend_View();
    $view->setScriptPath($viewDir);
    $view->carDamage = $carDamage;
    $view->estimatedBudget = $estimatedBudget;
    $template = DamagesEmailTemplatesQuery::create()->findOneByCode(DamagesEmailTemplates::CODE_DEALER_PERPETRATOR_ACCEPT);
    $templateContent = $template->getTemplate();
    $templateContent = nl2br($templateContent);
    foreach (Damages_Tags::getInstance()->getList() as $value) {
      if (preg_match(sprintf(Damages_Emails::TEMPLATE_REGEX, $value->code), $templateContent)) {
        $data = $view->render($value->code . '.phtml');
        $templateContent = preg_replace(sprintf(Damages_Emails::TEMPLATE_REGEX, $value->code), $data, $templateContent);
      }
    }
    $dealerEmail = '';
    foreach ($carDamage->getCarData()->getDealersRelatedByDealerId()->getUserDealerss() as $userDealer) {
      $dealerEmail = $dealerEmail ? sprintf('%s,%s', $dealerEmail, $userDealer->getUsers()->getEmail()) : $userDealer->getUsers()->getEmail();
    }
//    $dealerEmail = $carDamage->getCarData()->getDealersRelatedByDealerId()->getUsers()->getEmail();
    Damages_Emails::addEmail($carDamage->getId(), $template->getId(), $templateContent, $dealerEmail);
  }

}